package yxy.game.pm2.module.tip.checker;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import yxy.apple.util.PubSub.MethodSubscriber;
import yxy.apple.util.PubSub.SubscriptionMethod;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.weaponrefine;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.pubsub.CurrencyPublisher;
import yxy.game.pm2.bean.pubsub.CurrencyPublisher.CurrencySubscription;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.niubee.NiuBeeModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

public class TipSubscriber_神器精炼 extends MethodSubscriber {

	static private TipSubscriber_神器精炼 instance;

	public static TipSubscriber_神器精炼 getInstance() {
		if (instance == null) {
			instance = new TipSubscriber_神器精炼();
			instance.init();
		}
		return instance;
	}

	private void init() {
		CurrencyPublisher.getInstance().subscribe(this);
	}

	private TipSubscriber_神器精炼() {
	}

	@SubscriptionMethod(Type = CurrencySubscription.class)
	public void receive(CurrencySubscription subscription) {
//		System.err.println("订阅更新Task");
		String playerCode = subscription.getPlayerCode();
		if (!subscription.checkTypes(allowType())) {
			return;
		}
		new TipHandle().updateTip(playerCode, TipType.神器_精炼切页, check(playerCode));
	}

	private boolean check(String playerCode) {
		NiuBeeModule module = NiuBeeModule.get(playerCode);
		if (module.getProgress() < 5) {
			return false;
		}
		int refine = module.getRefineLevel();
		weaponrefine data = DataCenter.getData(refine + 1, weaponrefine.class);
		if (data == null) {
			return false;// 满级
		}
		data = DataCenter.getData(refine, weaponrefine.class);
		Collection<Currency> cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());

		boolean cancost = new CurrencyHandle().costCheck(playerCode, cost);
		return cancost;
	}

	private Set<CurrencyType> allowTypes;

	private Set<CurrencyType> allowType() {
		if (allowTypes != null) {
			return allowTypes;
		}
		allowTypes = new HashSet<>();

		Collection<weaponrefine> datas = DataCenter.getAllData(weaponrefine.class).values();
		for (weaponrefine data : datas) {
			for (int i = 0; i < data.getCosttype().length; i++) {
				CurrencyType type = CurrencyType.forNumber(data.getCosttype()[i]);
				if (type != null) {
					allowTypes.add(type);
				}
			}
		}

		return allowTypes;
	}
}
